package com.gzy.cbrsystem.shimian.action;

import org.apache.lucene.search.*;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.mira.lucene.analysis.IK_CAnalyzer;

import java.util.ArrayList;
public class KeyWordSearch {
	IK_CAnalyzer analyzer = new IK_CAnalyzer();
	String INDEX_PATH = "/CaseProject/WebRoot/WEB-INF";
	IndexReader reader;
	IndexWriter writer;
	
	public ArrayList<String> searchAnd(ArrayList a) {
		ArrayList<String> rtid = new ArrayList();
		try {
			reader = IndexReader.open(INDEX_PATH);
			QueryParser exprParser = new QueryParser("EXPR", analyzer);
	   
			Query q;
			BooleanQuery query = new BooleanQuery();
    
			ArrayList list = new ArrayList();
			for(int j = 0; j<a.size(); j++){
				list.add(a.get(j).toString());
			}	
			for(int i = 0; i < list.size(); i++) {
		    	q = exprParser.parse(list.get(i).toString());
		    	query.add(q, BooleanClause.Occur.MUST);
		    }
		    
		    System.out.println(query.toString());
		    Searcher searcher = new IndexSearcher(reader);
		    Hits hits = searcher.search(query);
		    if (hits.length() == 0) {
		    	System.out.println("hits.length=0");
		    }
		    /*for(int i=0; i<hits.length(); i++) {
		    	Document doc = hits.doc(i);
		    	String pid = doc.get("ID");
		    	String pexpr = doc.get("EXPR");
		    	System.out.println(pid + "   "  + pexpr);
		    	rtid.add(pid);
		    } 	*/
			}catch(Exception e) {
				e.printStackTrace();
			}
			finally{
				try{
					if(reader!=null){
						reader=null;
						reader.close();
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			return rtid;
}

/*void searchOr(ArrayList a) {
	try {
		reader = IndexReader.open(INDEX_PATH);
	    QueryParser exprParser = new QueryParser("EXPR", analyzer);
	   
	    Query q;
	    BooleanQuery query = new BooleanQuery();
    
		ArrayList list = new ArrayList();
        for(int j = 0; j<a.size(); j++){
        	list.add(a.get(j).toString());
        }
	    		
	    for(int i = 0; i < list.size(); i++) {
	    	q = exprParser.parse(list.get(i).toString());
	    	query.add(q, BooleanClause.Occur.SHOULD);
	    }
	    
	    System.out.println(query.toString());
		
	    Searcher searcher = new IndexSearcher(reader);
	    Hits hits = searcher.search(query);
	    if (hits.length() == 0) {
	    	System.out.println("hits.length=0");
	    }
	    for(int i=0; i<hits.length(); i++) {
	    	Document doc = hits.doc(i);
	    	String pid = doc.get("ID");
	    	String pexpr = doc.get("EXPR");
	    	System.out.println(pid + "   "  + pexpr);
	    	rtid.add(pid);
	    } 	
	    reader.close();
	    
	}catch(Exception e) {
		System.out.println(e.getMessage());
		e.printStackTrace();
	}
}*/
}
